/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.easoftware.code.dao;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.inject.Named;
import javax.sql.DataSource;
import net.easoftware.code.error.EaError;
import org.apache.commons.dbutils.QueryRunner;

/**
 *
 * @author waumovil-javaengineer
 */
@Named("dsManager")
public class DSManager implements Serializable {

    @Resource(lookup = "jdbc/micreditdbOpt")
    private DataSource ds;

    public DataSource getDs() {
        return ds;
    }

    /**
     *
     * @return @throws EaError
     */
    public Connection getConnection() throws EaError {
        try {
            return ds.getConnection();
        }
        catch (SQLException ex) {
            throw new EaError("Error obteniendo la conexion", ex);
        }
    }

    /**
     *
     * @param sql
     * @param params
     * @return
     * @throws EaError
     */
    public boolean doUpdate(String sql, Object... params) throws EaError {
        try {
            QueryRunner runner = new QueryRunner(ds);
            runner.update(sql, params);

            return true;
        }
        catch (SQLException ex) {
            Logger.getLogger(DSManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
}
